package com.miao.teacher.ui;

import com.miao.teacher.utils.TMediaPlayer;
import com.miao.teacher.widget.ExamplePlayer;

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ExampleFragment extends BaseFragment {

	public String[] exampleArr = new String[] { "c_1.amr", "r_2.amr",
			"c_3.amr", "r_4.amr", "c_5.amr", "r_6.amr", "c_7.amr", "r_8.amr",
			"c_9.amr", "r_10.amr", "c_11.amr", "r_12.amr", "c_13.amr",
			"r_14.amr", "c_15.amr" };
	public String overallAmr = "r_overall.amr";
	public String suggestionAmr = "r_suggestion.amr";

	public static ExampleFragment newInstance() {
		ExampleFragment fragment = new ExampleFragment();
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.layout_example, null);
		initView(view);
		return view;
	}
	AnimationDrawable playAnim = null;
	TMediaPlayer mVoicePlayer = null;
	ImageView animComment;
	ImageView animAdvise;
	private void initView(final View view) {
		mVoicePlayer = new TMediaPlayer();
		final LinearLayout lly = (LinearLayout) view
				.findViewById(R.id.lly_timeline);
		ImageView cImg = (ImageView) view.findViewById(R.id.c_seekbar);
		ImageView rImg = (ImageView) view.findViewById(R.id.r_seekbar);
		final ExamplePlayer p = new ExamplePlayer(getActivity(), mHandler,
				exampleArr, getXPx() - dip2px(32), lly, cImg, rImg);
		view.findViewById(R.id.btnPlayerPlay).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (p.isPlaying()) {
							p.doPause();
							view
									.findViewById(R.id.btnPlayerPlay)
									.setBackgroundDrawable(
											getActivity()
													.getResources()
													.getDrawable(
															R.drawable.iv_audio_play));
						} else {
							p.doRestart();
							view
									.findViewById(R.id.btnPlayerPlay)
									.setBackgroundDrawable(
											getActivity()
													.getResources()
													.getDrawable(
															R.drawable.iv_audio_pause));
						}
					}
				});
		final LinearLayout llyAudio = (LinearLayout) view
				.findViewById(R.id.lly_audio_comment);
		final LinearLayout llyAudioAdvise = (LinearLayout) view
				.findViewById(R.id.lly_audio_advise);
		animComment = (ImageView) view
				.findViewById(R.id.imgAudio_comment);
		animAdvise = (ImageView) view
				.findViewById(R.id.imgAudio_advise);
		llyAudio.setOnClickListener(new CommentPlay());
		llyAudioAdvise.setOnClickListener(new AdvisePlay());
	}
	/*
	 * 播放评论录音
	 */
	class CommentPlay implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try {
				AssetManager assetManager = getActivity().getAssets();
				AssetFileDescriptor afd = assetManager.openFd("r/"
						+ overallAmr);
				mVoicePlayer.ASycPlay(afd, mHandler);
				animComment
						.setBackgroundResource(R.drawable.anim_revise_playing);
				if (playAnim != null) {
					playAnim.stop();
				}
				playAnim = (AnimationDrawable) animComment.getBackground();
				playAnim.start();
			} catch (Exception er) {
			}
		}

	}
	/*
	 * 播放建议录音
	 */
	class AdvisePlay implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try {
				AssetManager assetManager = getActivity().getAssets();
				AssetFileDescriptor afd = assetManager.openFd("r/"
						+ suggestionAmr);
				mVoicePlayer.ASycPlay(afd, mHandler);
				animAdvise
						.setBackgroundResource(R.drawable.anim_revise_playing);
				if (playAnim != null) {
					playAnim.stop();
				}
				playAnim = (AnimationDrawable) animAdvise.getBackground();
				playAnim.start();
			} catch (Exception er) {
			}
		}

	}

}
